package com.hyw.supply.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hyw.supply.model.entity.SupplyItem;
import com.hyw.supply.model.entity.SupplyOrder;
import com.hyw.supply.model.query.OrderListQuery;
import com.hyw.supply.model.vo.*;
import com.hyw.supply.service.SupplyItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 供应商项目信息 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2024-03-15
 */
@RestController
@RequestMapping("/supplyItem")
@RequiredArgsConstructor
@Tag(name = "代理发货订单列表")
public class SupplyItemController {


    private final SupplyItemService supplyItemService;

    @Operation(summary = "列表查询接口",hidden = true)
    @PostMapping("/pageList")
    public CommonResult<PageVO<SupplyItemListVO>> pageList(@RequestBody OrderListQuery listQuery) {
        IPage<SupplyItem> page = supplyItemService.lambdaQuery()
                .eq(StringUtils.isNotBlank(listQuery.getOrderStatus()), SupplyItem::getOrderStatus, listQuery.getOrderStatus())
                .eq(StringUtils.isNotBlank(listQuery.getItemOrderNo()), SupplyItem::getItemOrderNo, listQuery.getItemOrderNo())
                .eq(StringUtils.isNotBlank(listQuery.getChildOrderNo()), SupplyItem::getChildOrderNo, listQuery.getChildOrderNo())
                .eq(StringUtils.isNotBlank(listQuery.getSkuCode()), SupplyItem::getSkuCode, listQuery.getSkuCode())
                .like(StringUtils.isNotBlank(listQuery.getGoodsName()), SupplyItem::getGoodsName, listQuery.getGoodsName())
                .eq(StringUtils.isNotBlank(listQuery.getItemCode()), SupplyItem::getItemCode, listQuery.getItemCode())
                .eq(StringUtils.isNotBlank(listQuery.getEnterprise()), SupplyItem::getEnterprise, listQuery.getEnterprise())
                .ge(StringUtils.isNotBlank(listQuery.getStartDeliveryDate()), SupplyItem::getDeliveryDate, listQuery.getStartDeliveryDate())
                .le(StringUtils.isNotBlank(listQuery.getEndDeliveryDate()), SupplyItem::getDeliveryDate, listQuery.getEndDeliveryDate())
                .orderByDesc(SupplyItem::getDeliveryDate).orderByDesc(SupplyItem::getId)
                .page(listQuery.toPage());

        PageVO<SupplyItem> supplyItemPage = PageVO.convertPage(page);
        List<SupplyItemListVO> list = supplyItemPage.getData().stream().map(item -> {
            SupplyItemListVO supplyItem = new SupplyItemListVO();
            BeanUtils.copyProperties(item, supplyItem);
            return supplyItem;
        }).toList();

        PageVO<SupplyItemListVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(supplyItemPage, pageVO);
        pageVO.setData(list);
        return CommonResult.success(pageVO);
    }

    @Operation(summary = "详情接口查询",hidden = true)
    @GetMapping("/detail/{id}")
    @Parameter(name = "id", description = "详情id", in = ParameterIn.PATH)
    public CommonResult<SupplyItemDetailVO> pageList(@PathVariable Long id) {
        SupplyItem supplyItem = supplyItemService.getById(id);
        SupplyItemDetailVO supplyItemDetailVO = new SupplyItemDetailVO();
        BeanUtils.copyProperties(supplyItem, supplyItemDetailVO);
        return CommonResult.success(supplyItemDetailVO);
    }


    @Operation(summary = "下拉框值",hidden = true)
    @GetMapping("/dropown")
    public CommonResult<DropDownVO> dropownList() {
        List<SupplyItem> supplyItemList = supplyItemService.lambdaQuery()
                .select(SupplyItem::getOrderStatus, SupplyItem::getEnterprise)
                .list();

        List<DropDown> orderStausList = supplyItemList.stream().map(SupplyItem::getOrderStatus)
                .distinct()
                .map(item -> new DropDown(item, item)).toList();

        List<DropDown> enterpriseList = supplyItemList.stream().map(SupplyItem::getEnterprise)
                .distinct()
                .map(item -> new DropDown(item, item)).toList();

        return CommonResult.success(new DropDownVO(orderStausList, enterpriseList));
    }


}
